Ext.setup({
    icon:'icon.png',
    tabletStartupScreen:'tablet_startup.png',
    phoneStartupScreen:'phone_startup.png',
    glossOnIcon:false,
    onReady:function () {
        var form;
        
        Ext.regModel('DadilyCostData', {
            fields: [
                {name: 'BILLCODE',     type: 'string'},
                {name: 'AMOUNT', type: 'password'},
                {name: 'COMMITER',    type: 'string'},
                {name: 'COMMITERCODE',      type: 'string'},
                {name: 'DEPARTMENT',     type: 'string'},
                {name: 'APPLYDATE',     type: 'date'},
                {name: 'DESCRIBE',   type: 'string'},
                {name: 'APPLIERNAME',    type: 'string'},
                {name: 'OWNERDEPT',     type: 'string'}
            ],
            validations: [ 
       	               	  {type: 'presence', name: 'COMMITER'},
    		              {type: 'presence', name: 'OWNERDEPT'},
    		              {type: 'presence', name: 'APPLIERNAME'},
    		              {type: 'presence', name: 'DESCRIBE'},
    		              {type: 'presence', name: 'COMMITERCODE'}
    		          ]
        });
        
        var params=window.location.href.split("=");
        var param='';
        var billcode='';
        if(params[1].indexOf("&")==-1){
        	param=params[1];
        }else{
        	param=params[1].split("&")[0];
        	billcode=params[2];
        }
        Ext.Ajax.request({        	
            url:'../../costmanagement/dailyListCost_getCostDetailByBillcode.action?TYPE=002&BILLCODE='+billcode+'&COMMITERCODE='+param,
            success:function (response, opts) {
            	var res=JSON.parse(response.responseText);
            	formBase.dadilyCostData = Ext.ModelMgr.create({
                    'BILLCODE'    : res.BILLCODE,
                    'AMOUNT': res.AMOUNT,
                    'COMMITER'   : res.COMMITER,
                    'COMMITERCODE' : res.COMMITERCODE,
                    'DEPARTMENT': res.DEPARTMENT,
                    'DESCRIBE'  : res.DESCRIBE,
                    'APPLIERNAME'    : res.APPLIERNAME,
                    'OWNERDEPT'   : res.OWNERDEPT
                },  'DadilyCostData');

                form.loadModel(formBase.dadilyCostData);
            }
        });
	   	 Ext.regModel('agenymapmodel', {
	   	    fields: [
	   	        {name: 'AGENCYID', type: 'string'},
	   	        {name: 'NAME', type: 'string'}
	   	    ]
	   	});
		
	   	Ext.regModel('userModel', {
		   	    fields: [
		   	        {name: 'ID', type: 'string'},
		   	        {name: 'NAME', type: 'string'}
		   	    ]
		});
	   	
		var agenymapStore = new Ext.data.Store({
			autoLoad: true,
		    model: 'agenymapmodel',
		    proxy: {
		        type: 'ajax',
		        url : '../../dic/dic_agencyMap.action',
		        reader: {
		            type: 'json'
		        }
		    }
		});
		
		var requestusers = new Ext.data.Store({
			autoLoad: true,
		    model: 'userModel',
		    proxy: {
		        type: 'ajax',
		        url : '../../costmanagement/dailyListCost_getRequestUser.action?COMMITERCODE='+param,
		        reader: {
		            type: 'json',
		            root:'requestuserlist'
		        }
		    }
		});
		
        var formBase = {
            scroll:'vertical',
            standardSubmit:false,
            items:[
                {
                    xtype:'fieldset',
                    title:'所属单位',
                    items:[
                        {
                            xtype:'selectfield',
                            name:'OWNERDEPT',
                            label:'单位名称',                            
    						placeHolder : '请选择所属单位',
    						valueField: 'AGENCYID',
    						displayField:'NAME',
    						store : agenymapStore,
    						required : true
                        }
                    ]
                },
                {
                    xtype:'fieldset',
                    title:'基本信息',
                    instructions:'请输入报销编号',
                    defaults:{
                        labelAlign:'left',
                        labelWidth:'40%'
                    },
                    items:[
                        {
                            xtype:'textfield',
                            name:'BILLCODE',
                            label:'报销编号',
                            disabled: true,
                            required:false,
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textfield',
                            name:'DEPARTMENT',
                            label:'提交人部门',
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textfield',
                            name:'COMMITER',
                            label:'提交人',
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textfield',
                            name:'COMMITERCODE',
                            label:'提交人编号',
                            value:'',
                            useClearIcon:false
                        }
                    ]
                },
                {
                    xtype:'fieldset',
                    title:'报销信息',
                    defaults:{
                    	required:true,
                        xtype:'fieldset'
                    },
                    items:[
                        {
                            xtype:'selectfield',
                            name:'APPLIERNAME',
                            label:'申请人',
    						placeHolder : '请选择所申请人',
    						valueField: 'ID',
    						displayField:'NAME',
    						store : requestusers
                        },
                        {
                            xtype:'textfield',
                            name:'AMOUNT',
                            label:'申请金额',
                            disabled:true,
                            value:'0',
                            useClearIcon:false
                        },
                        {
                            xtype:'textareafield',
                            name:'DESCRIBE',
                            label:'申请事由',
                            maxLength:60,
                            maxRows:3
                        }
                    ]
                }
            ],
            listeners:{
                submit:function (form, result) {
                    console.log('success', Ext.toArray(arguments));
                },
                exception:function (form, result) {
                    console.log('failure', Ext.toArray(arguments));
                }
            },

            dockedItems:[
                {
                    xtype:'toolbar',
                    dock:'bottom',
                    items:[
                        {
                            xtype:'spacer'
                        },
                        {
                            text:'保存',
                            ui:'confirm',
                            handler:function () {
        						var currentNote = form.getRecord(); 
        			            form.updateRecord(currentNote); 		 
        		                var errors = currentNote.validate(); 
        		                if (!errors.isValid()) { 
        		                Ext.Msg.alert('等待...', "带*号选项为必填信息！", Ext.emptyFn); 
        		                    return; 
        		                }
                            	Ext.Msg.confirm('系统提示','确定要提交？',function(btn){
                    		        if(btn=='yes'){
                    		        		var url;
                    		        		url="../../costmanagement/dailyCostinsert.action?Type=001&COMMITERCODE="+param;
              								formBase.submitform(url);
                    		        }else{
                    		        	return;
                    		        }
                            		        
                            	},this);
                            }
                        }
                    ]
                }
            ],
            
            submitform:function(url){
            	form.submit( {
    				waitMsg : {
    					message : '正在提交...',
    					cls : 'demos-loading'
    				},
    			 url: url,           
    			 method: 'POST',           
    			 success: function(thisForm, action){ 
    					Ext.Msg.alert('提示', '提交成功',function(btn){
    						window.location.href='../../costmanagement/dailyCostSubmit/regulardailyCostSubmit.jsp?COMMITERCODE=014&ID='+action.FORM.BILLCODE;
    					});	
    					
    				}
    			});
            }
        };
        
        if (Ext.is.Phone) {
            formBase.fullscreen = true;
        } else {
            Ext.apply(formBase, {
                autoRender:true,
                floating:true,
                modal:true,
                centered:true,
                hideOnMaskTap:false,
                height:385,
                width:480
            });
        }

        form = new Ext.form.FormPanel(formBase);
        form.show();
    }
});